package com.woniuxy.enjoylife.controller;

import com.woniuxy.enjoylife.entity.Community;
import com.woniuxy.enjoylife.entity.dto.ActivityDto;
import com.woniuxy.enjoylife.enums.StateEnum;
import com.woniuxy.enjoylife.service.ActivityService;
import com.woniuxy.enjoylife.service.CommunityService;
import com.woniuxy.enjoylife.utils.PageResult;
import com.woniuxy.enjoylife.utils.ResponseResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.LinkedList;
import java.util.List;

/**
 * 小区控制器
 */
@RestController
@RequestMapping("/community")
public class CommunityController {
    @Resource
    private CommunityService communityService;
    @Resource
    private ActivityService activityService;
    @Resource
    private HttpServletRequest request;

    /**
     * 物业查看本小区，通过小区去查活动
     */
    @RequestMapping("/findCommunityActivitysByWid")
    public ResponseResult<List> findCommunityActivitysByWid(int current, int size){

        //根据登录获取
        int wid = (int) request.getSession().getAttribute("id");
        //根据物业id查找小区信息
        Community community = communityService.findCommunityByWid(wid);
        //获取小区id
        Integer cid = community.getId();
        //根据小区id获取小区活动以及发起人，并分页
        PageResult<List<ActivityDto>> pageResult = activityService.findActivitysByCid(cid, current, size);
        //用一个List封装小区信息和活动
        List list = new LinkedList();
        //装入list
        list.add(community);
        list.add(pageResult);
        //返回ResponseResult
        ResponseResult responseResult = new ResponseResult();
        if(community==null || pageResult ==null){
            responseResult.error();
        }else {
            responseResult.success();
            responseResult.setData(list);
        }
        return responseResult;
    }

    /**
     * 查询此小区能否注册
     * @param community
     * @return
     */
    @RequestMapping("/haveCommunity")
    public ResponseResult haveCommunity(String community){
        ResponseResult<Object> responseResult = new ResponseResult<>();
        if (communityService.haveCommunity(community)){
            //能注册
            responseResult.setStatusCode(200);
            responseResult.setMessage("SUCCESS");
            responseResult.setState(StateEnum.SUCCESS);
        }else {
            //不能注册
            responseResult.setStatusCode(501);
            responseResult.setMessage("FALSE");
            responseResult.setState(StateEnum.ERROR);
        }
        return responseResult;
    }

    /**
     * 查询用户拥有房屋对应的小区
     * @return
     */
    @RequestMapping("/findUserCommunity")
    @ResponseBody
    public ResponseResult<List<Community>> findUserCommunity(){
        //用户 uid
        int uid = (int) request.getSession().getAttribute("id");
        ResponseResult<List<Community>> responseResult = new ResponseResult<>();
        List<Community> list = communityService.findUserCommunity(uid);
        for (Community com:list) {
            System.out.println(com);
        }
        responseResult.setData(list);
        if(list.size()>0){
            responseResult.success();
        }else {
            responseResult.error();
        }
        return responseResult;
    }

}
